$(function () {
    var layer = layui.layer;
    getUserInfo();

    function getUserInfo() {
        $.ajax({
            method: 'get',
            url: '/my/userinfo',
            success: function (res) {
                if (res.status !== 0) {
                    return layui.layer.msg(res.message, {
                        icon: 5
                    });

                }
                renderAvatar(res.data);
            },
        })
    }

    function renderAvatar(user) {
        var name = user.nickname || user.username;
        $('#welcome').html('欢迎您：' + name);
        if (user.user_pic) {
            $('.layui-nav-img').attr('src', user.user_pic).show();
            $('.text-avatar').hide();
        } else {
            var first = name.substr(0, 1).toUpperCase();
            $('.text-avatar').html(first).show();
            $('.layui-nav-img').hide();
        }
    }
    $('#logout').on('click', function () {
        layer.confirm('您确定要退出吗?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            localStorage.removeItem('token');
            location.href = '/login.html';
            layer.close(index);
        })
    })
})